home *** CD-ROM | disk | FTP | other *** search
- Path: news1.interserv.net!news
- From: <dvisage@interserv.com>
- Newsgroups: comp.lang.c++
- Subject: Convert string to char*
- Date: 16 Feb 1996 11:08:42 GMT
- Organization: InterServ News Service
- Message-ID: <4g1ojq$r4r@lal.interserv.net>
- NNTP-Posting-Host: dd76-132.compuserve.com
- Content-Type: text/plain
- Content-length: 1463
- X-Newsreader: AIR Mosaic (32-bit) 4.00
-
-
- >something like:
- >
- > char* operator ?? ()
- >
- >so that my string class can also return char*. Please help. Thanks,
- >
- >Abu Wawda
- >wawda@scf.usc.edu
- >
-
- class YourStringClass
- {
- char* _data; // Points to string data
-
- public :
-
- operator char* () { return _data; }
- };
-
-
- There are several problems here :
-
- (1) Why in the world would you create your string class when eveyone and
- their mother (except mine) has already written one? In fact, there is
- already an ANSI standard C++ string class. It is derived from a template
- call basic_string<>. I won't go all the gory details, but I am sure that it
- will suit most of your needs. You should be able to get a copy of
- bstring.h by downloading the most recent version of STL.
-
- (2) User defined conversions are inherently dangerous
-
- For example, assume the following code :
-
- YourStringClass str;
-
- cout << str << endl;
-
- Unless you make sure to define :
-
- ostream& operator<<(ostream& os, const YourStringClass& str);
-
- The compiler will automatically call your operator char* () function
- and then will call ostream::operator<<(char *).
-
- If this is not what you want, you are SOL.
-
- P.S - This is the reason that the ANSI string class requires you to call
- the member function data() to get the chararacter string pointer
- of a string data type. It used to be called c_str().
-
-